/*var rd = require('rd');

// 异步列出目录下的所有文件
rd.read('common', function (err, files) {
  if (err) throw err;
  console.log(files);
  // files是一个数组，里面是目录/tmp目录下的所有文件（包括子目录）
});*/



var PDF = require('pdfkit'); //including the pdfkit module
var fs = require('fs');


function readLines(input, func) {
    var remaining = '';
    input.on('data', function(data) {
        console.log(111);
        remaining += data;
        var index = remaining.indexOf('\n');
        while (index > -1) {
            var line = remaining.substring(0, index);
            remaining = remaining.substring(index + 1);
            func(line);
            index = remaining.indexOf('\n');
        }

    });

    input.on('end', function() {
        console.log(222);
        if (remaining.length > 0) {
            func(remaining);
        }
    });
}

function func(data) {
    // console.log(data);
    // container.push(data);
    // 
    doc = new PDF();
    // doc.font('/Windows/Fonts/simsunb.ttf').fontSize(24);
    //creating a new PDF object
    doc.pipe(fs.createWriteStream('demo.pdf')); //creating a write stream 
    //to write the content on the file system
/*    doc.addPage()
       .fontSize(25)
       .text(data, 0, 0)*/
    // console.log(data);
    doc.text(data, 100, 100); //adding the text to be written, 
    // more things can be added here including new pages
    doc.end(); //we end the document writing.
}

var input = fs.createReadStream(__dirname + '/common/js/common.js');

readLines(input, func);
